package com.lexed.usersys.utils;

import java.io.*;
import java.util.List;

import com.lexed.usersys.entity.User;

public class FileFunctions {
	/**
	 * 发送文件功能
	 * 
	 * @author Administrator
	 *
	 */
	public void sendFile(String src, String dest, String name, String getterName) throws FileNotFoundException {
		// 打开流
		FileInputStream f1 = null;
		FileOutputStream f2 = null;
		byte[] buf = new byte[(int) new File(src).length()];
		try {
			f1 = new FileInputStream(src);
			f2 = new FileOutputStream(dest);
			// 读取文件
			f1.read(buf);
			// 把文件发送到对方路径
			f2.write(buf, 0, buf.length);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("发送失败！");
		} finally {
			// 关闭
			try {
				f1.close();
				f2.close();
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("关闭失败！");
			}

		}
		// 提示信息
		System.out.println("\n" + name + "给" + getterName + "发送文件 " + src + " 到对方的电脑目录 " + dest);
	}

	/**
	 * 保存文件功能
	 */
	public void saveFile(String src2, List<User> list) {
		// 打开流
		try {
			BufferedWriter w = new BufferedWriter(new FileWriter(src2));
			// 保存信息
			w.write("用户id\t用户名\t密码\t  性别\t年龄");
			for (User u : list) {
				w.newLine();
				w.flush();
				w.write(u.getId() + "\t" + u.getName() + "\t" + u.getPwd() + "\t" + u.getSex() + "\t" + u.getAge());
			}
			System.out.println("保存到 " + src2 + " 成功,请查看");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
